﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;

namespace VSSpeeder.Utils
{
	public class CommandWithButton
	{
		public string Name { get; set; }
		public string Caption { get; set; }
		public string Tooltip { get; set; }
		public int FaceId { get; set; }
		public string AddToToolbarName { get; set; }
		public string AddToMenuName { get; set; }
		public string ShortCutKey { get; set; }

		public Command Command {get;private set;}
		public CommandBarButton ToolbarButton {get; private set; }	// Debug ToolBar Button
		public CommandBarButton MenuButton {get;private set; }	// Build Menu Button

		// 579 - Cancel or 1670
		// 56 - Enable Builds
		public CommandWithButton(string name, string text, string tooltip, int faceId,
			string addToToolbarName, string addToMenuName, string shortCutKey)
		{
			Name = name;
			Caption = text;
			Tooltip = tooltip;
			FaceId = faceId;
			AddToToolbarName = addToToolbarName;
			AddToMenuName = addToMenuName;
			ShortCutKey = shortCutKey;
		}

		public void Initialize(DTE2 application, AddIn addin)
		{
			var contextGuids = new object[] { };
			var commands = (Commands2)application.Commands;
			CommandBar menuBarCommandBar = ((CommandBars)application.CommandBars)["MenuBar"];

			try
			{
				Command = application.Commands.Item(addin.ProgID + "." + Name, -1);
			}
			catch
			{
			}

			// Add the command if it does not exist
			if (Command == null)
			{
				Command = commands.AddNamedCommand2(
					addin, Name, Caption, Tooltip, true, FaceId, ref contextGuids,
					(int) (vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled),
					(int) vsCommandStyle.vsCommandStylePict,
					vsCommandControlType.vsCommandControlTypeButton);
			}

			if (ShortCutKey != null)
			{
				var bindings = new object[1];
				bindings[0] = (object)ShortCutKey;
				try
				{
					Command.Bindings = bindings;
				}
				catch (Exception ex)
				{
					Trace.WriteLine(ex.Message);
				}
			}

			if(AddToToolbarName!=null)
			{	// Add to Toolbar
				CommandBar toolBar = ((CommandBars)application.CommandBars)[AddToToolbarName];
				// try to delete the old command
				foreach (CommandBarControl barButton in toolBar.Controls)
				{
					if (barButton.TooltipText == Tooltip)
					{
						barButton.Delete();
						break;
					}
				}

				ToolbarButton = (CommandBarButton)Command.AddControl(toolBar, toolBar.Controls.Count + 1);
				ToolbarButton.Style = MsoButtonStyle.msoButtonIcon;
			}

			if (AddToMenuName != null)
			{
				CommandBarPopup menuBar = (CommandBarPopup) menuBarCommandBar.Controls[AddToMenuName];
				foreach (CommandBarControl barButton in menuBar.Controls)
				{
					if (barButton.TooltipText == Tooltip)
					{
						barButton.Delete();
						break;
					}
				}

				MenuButton = (CommandBarButton) Command.AddControl(menuBar.CommandBar, menuBar.CommandBar.Controls.Count - 1);
			}
		}

		public void Delete()
		{
			if (ToolbarButton != null)
				ToolbarButton.Delete();
			if (MenuButton != null)
				MenuButton.Delete();
		}

		public void Refresh()
		{
			if (ToolbarButton != null)
			{
				ToolbarButton.Visible = false;
				ToolbarButton.Visible = true;
			}
		}

		internal void ChangeCaption(string newCaption)
		{
			Caption = newCaption;
			if (ToolbarButton != null)
				ToolbarButton.Caption = newCaption;
			if (MenuButton != null)
				MenuButton.Caption = newCaption;
		}
	}
}
